package exer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author tongchen
 * @create 2023-02-08 16:34
 */
public class ArraylistWithMultithreading {
   static Object loker=new Object();
    public static void main(String[] args) {
        //创建集合
        ArrayList<Integer> arrayList = new ArrayList<>();
        List<Integer> list = Collections.synchronizedList(arrayList);
        ReentrantLock reentrantLock = new ReentrantLock();
        CopyOnWriteArrayList<Integer> integers = new CopyOnWriteArrayList<>(arrayList);
        //创建任务并加入多线程
        for (int i = 0; i < 11; i++) {


                int x=i;
                Thread  thread=new Thread(()->{

                    reentrantLock.lock();
                        arrayList.add(x);
                    reentrantLock.unlock();


                });
            thread.start();
            reentrantLock.lock();
            System.out.println(arrayList);
            reentrantLock.unlock();

        }
        System.out.println("-----------------------");
    }
}
